package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.pojo.Commodity;
import com.pojo.Employee;
import com.service.CommodityService;

@Controller
@RequestMapping("/commodity")
public class CommodityController {

	@Autowired
	private CommodityService commodityService;
	
	@RequestMapping(value = "/list")
	public String listUsers(Model model) {
		System.out.println("commodity/list");
		List<Commodity> commoditys = commodityService.GetAllCommodity();
		//String name;
		for(Commodity p :commoditys) {
//			name=commodityService.GetEmployee(p.getPro_Emp_id()).getEmployee_Name();
//			p.setName(name);
			System.out.println(p);
		}
		model.addAttribute("commoditys", commoditys);
		return "/jsp/commodity";
	}
	
	@RequestMapping(value = "/tocommodity_add")
	public String toKaoqin_add(Model model) {
		model.addAttribute("commodity", new Commodity());
		return "/jsp/commodity_add";
	}

	@RequestMapping(value = "/commodity_add")
	public String Kaoqin_add(@ModelAttribute Commodity commodity, Model model) {
		String url = "";
		boolean result=true;
		//找编号
		List<Commodity> comms=commodityService.GetAllCommodity();
		for(Commodity c: comms) {
			if(c.getCommodity_Id()==commodity.getCommodity_Id())
				result=false;
		}
		//编号存在
		if(result) {
			if(commodityService.addCommodity(commodity))
				url = "redirect:../commodity/list";
			else {
				String error = "添加失败";
				model.addAttribute("commodity", commodity);
				model.addAttribute("error", error);
				url = "jsp/commodity_add";
			}
			
		}else {
			String error = "添加失败";
			model.addAttribute("commodity", commodity);
			model.addAttribute("error", error);
			url = "jsp/commodity_add";
		}
		return url;
	}
	
	@RequestMapping(value = "/delete")
	public String delete(@RequestParam Integer cid,Model model) {
		String url="";
		System.out.println("删除"+cid);
		if(commodityService.deletCommodityForId(cid))
			url= "redirect:../commodity/list";
		else {
			String error="删除失败";
			model.addAttribute("error",error);
			url= "redirect:../commodity/list";
		}
		return url;
	}
	
	@RequestMapping(value = "/toupdate")
	public String toupdate(@RequestParam Integer cid,Model model) {
		model.addAttribute("commodity",commodityService.GetCommodity(cid));
		return "/jsp/commodity_update";
	}

	@RequestMapping(value = "/update")
	public String update(@ModelAttribute Commodity commodity ,Model model) {
		String url="";
		System.out.println("更新"+commodity);
		if(commodityService.updateCommodity(commodity))
			url= "redirect:../commodity/list";
		else {
			String error="更新失败";
			model.addAttribute("error",error);
			url= "redirect:../commodity/list";
		}
		return url;
	}
	
	@RequestMapping(value = "/select")
	public String select(@RequestParam Integer id,Model model) {
		List<Commodity> coms=new ArrayList<>();
		Commodity c=commodityService.GetCommodity(id);
		System.out.println("选择"+c);
		if(c!=null) {
			coms.add(c);
			model.addAttribute("commoditys", coms);
		}else {
			String error="商品不存在";
			model.addAttribute("error",error);
			model.addAttribute("commoditys", coms);
		}

		return "/jsp/commodity";
	}
}
